import ApiService from './api';
import { PaginationParams, PaginatedResponse } from '../types';

// 用户查询参数
export interface UserQueryParams extends PaginationParams {
  keyword?: string;
  role?: string;
  status?: 'active' | 'inactive' | 'banned';
  isAdmin?: boolean;
}

// 用户数据
export interface User {
  _id: string;
  name: string;
  email: string;
  avatar?: string;
  phone?: string;
  role?: string;
  isAdmin?: boolean;
  status?: 'active' | 'inactive' | 'banned';
  createdAt: string;
  updatedAt: string;
}

// 用户创建/更新数据
export interface UserFormData {
  name: string;
  email: string;
  password?: string;
  avatar?: string;
  phone?: string;
  role?: string;
  isAdmin?: boolean;
  status?: 'active' | 'inactive' | 'banned';
}

// 用户服务
export class UserService {
  // 获取用户列表
  static async getUsers(params: UserQueryParams): Promise<PaginatedResponse<User>> {
    return await ApiService.get<PaginatedResponse<User>>('/admin/users', params);
  }

  // 获取用户详情
  static async getUser(id: string): Promise<User> {
    return await ApiService.get<User>(`/admin/users/${id}`);
  }

  // 创建用户
  static async createUser(data: UserFormData): Promise<User> {
    return await ApiService.post<User>('/admin/users', data);
  }

  // 更新用户
  static async updateUser(id: string, data: Partial<UserFormData>): Promise<User> {
    return await ApiService.put<User>(`/admin/users/${id}`, data);
  }

  // 删除用户
  static async deleteUser(id: string): Promise<void> {
    await ApiService.delete(`/admin/users/${id}`);
  }

  // 批量删除用户
  static async batchDeleteUsers(ids: string[]): Promise<void> {
    await ApiService.post('/admin/users/batch-delete', { ids });
  }

  // 更新用户状态
  static async updateUserStatus(id: string, status: 'active' | 'inactive' | 'banned'): Promise<User> {
    return await ApiService.patch<User>(`/admin/users/${id}/status`, { status });
  }

  // 批量更新用户状态
  static async batchUpdateUserStatus(ids: string[], status: 'active' | 'inactive' | 'banned'): Promise<void> {
    await ApiService.post('/admin/users/batch-status', { ids, status });
  }

  // 重置用户密码
  static async resetUserPassword(id: string, newPassword: string): Promise<void> {
    await ApiService.post(`/admin/users/${id}/reset-password`, { password: newPassword });
  }

  // 获取用户统计信息
  static async getUserStats(): Promise<{
    totalUsers: number;
    activeUsers: number;
    newUsersToday: number;
    bannedUsers: number;
  }> {
    return await ApiService.get('/admin/users/stats');
  }

  // 导出用户数据
  static async exportUsers(params: UserQueryParams): Promise<void> {
    await ApiService.download('/admin/users/export', 'users.xlsx');
  }

  // 获取用户活动日志
  static async getUserActivityLogs(id: string, params: PaginationParams): Promise<PaginatedResponse<any>> {
    return await ApiService.get<PaginatedResponse<any>>(`/admin/users/${id}/activity-logs`, params);
  }

  // 获取用户订单列表
  static async getUserOrders(id: string, params: PaginationParams): Promise<PaginatedResponse<any>> {
    return await ApiService.get<PaginatedResponse<any>>(`/admin/users/${id}/orders`, params);
  }

  // 获取用户购物车
  static async getUserCart(id: string): Promise<any> {
    return await ApiService.get(`/admin/users/${id}/cart`);
  }

  // 获取用户收藏列表
  static async getUserFavorites(id: string, params: PaginationParams): Promise<PaginatedResponse<any>> {
    return await ApiService.get<PaginatedResponse<any>>(`/admin/users/${id}/favorites`, params);
  }

  // 获取用户地址列表
  static async getUserAddresses(id: string): Promise<any[]> {
    return await ApiService.get<any[]>(`/admin/users/${id}/addresses`);
  }

  // 发送通知给用户
  static async sendNotificationToUser(id: string, data: {
    title: string;
    content: string;
    type?: 'info' | 'warning' | 'error' | 'success';
  }): Promise<void> {
    await ApiService.post(`/admin/users/${id}/notifications`, data);
  }

  // 批量发送通知
  static async sendBatchNotification(userIds: string[], data: {
    title: string;
    content: string;
    type?: 'info' | 'warning' | 'error' | 'success';
  }): Promise<void> {
    await ApiService.post('/admin/users/batch-notification', { userIds, ...data });
  }

  // 获取用户登录历史
  static async getUserLoginHistory(id: string, params: PaginationParams): Promise<PaginatedResponse<any>> {
    return await ApiService.get<PaginatedResponse<any>>(`/admin/users/${id}/login-history`, params);
  }

  // 强制用户下线
  static async forceUserLogout(id: string): Promise<void> {
    await ApiService.post(`/admin/users/${id}/force-logout`);
  }

  // 获取用户权限列表
  static async getUserPermissions(id: string): Promise<string[]> {
    return await ApiService.get<string[]>(`/admin/users/${id}/permissions`);
  }

  // 更新用户权限
  static async updateUserPermissions(id: string, permissions: string[]): Promise<void> {
    await ApiService.put(`/admin/users/${id}/permissions`, { permissions });
  }

  // 获取用户角色列表
  static async getUserRoles(): Promise<any[]> {
    return await ApiService.get<any[]>('/admin/users/roles');
  }

  // 分配角色给用户
  static async assignRoleToUser(userId: string, roleId: string): Promise<void> {
    await ApiService.post(`/admin/users/${userId}/roles`, { roleId });
  }

  // 移除用户角色
  static async removeRoleFromUser(userId: string, roleId: string): Promise<void> {
    await ApiService.delete(`/admin/users/${userId}/roles/${roleId}`);
  }

  // 获取在线用户列表
  static async getOnlineUsers(params: PaginationParams): Promise<PaginatedResponse<User>> {
    return await ApiService.get<PaginatedResponse<User>>('/admin/users/online', params);
  }

  // 获取用户地理分布
  static async getUserGeographicDistribution(): Promise<any[]> {
    return await ApiService.get<any[]>('/admin/users/geographic-distribution');
  }

  // 获取用户注册趋势
  static async getUserRegistrationTrend(period: 'week' | 'month' | 'quarter' = 'month'): Promise<any[]> {
    return await ApiService.get<any[]>('/admin/users/registration-trend', { period });
  }

  // 获取用户活跃度统计
  static async getUserActivityStats(period: 'week' | 'month' | 'quarter' = 'month'): Promise<any> {
    return await ApiService.get('/admin/users/activity-stats', { period });
  }

  // 搜索用户
  static async searchUsers(keyword: string, limit: number = 10): Promise<User[]> {
    return await ApiService.get<User[]>('/admin/users/search', { keyword, limit });
  }

  // 获取用户详细资料
  static async getUserProfile(id: string): Promise<any> {
    return await ApiService.get(`/admin/users/${id}/profile`);
  }

  // 更新用户详细资料
  static async updateUserProfile(id: string, data: any): Promise<any> {
    return await ApiService.put(`/admin/users/${id}/profile`, data);
  }

  // 验证用户邮箱
  static async verifyUserEmail(id: string): Promise<void> {
    await ApiService.post(`/admin/users/${id}/verify-email`);
  }

  // 验证用户手机号
  static async verifyUserPhone(id: string): Promise<void> {
    await ApiService.post(`/admin/users/${id}/verify-phone`);
  }

  // 获取用户实名认证信息
  static async getUserVerification(id: string): Promise<any> {
    return await ApiService.get(`/admin/users/${id}/verification`);
  }

  // 审核用户实名认证
  static async approveUserVerification(id: string, approved: boolean, reason?: string): Promise<void> {
    await ApiService.post(`/admin/users/${id}/verification/approve`, { approved, reason });
  }
}

export default UserService;
